package com.hh.service.service.impl.user;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.hh.common.enums.IntegralTransactionType;
import com.hh.common.model.integral.UserPointRecord;
import com.hh.common.page.CommonPage;
import com.hh.common.request.PageParamRequest;
import com.hh.common.response.integral.UserIntegralRecodResponse;
import com.hh.service.dao.integral.UserPointRecordDao;
import com.hh.service.service.UserService;
import com.hh.service.service.user.UserIntegralService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserIntegralServiceImpl implements UserIntegralService {

    @Autowired
    private UserService userService;

    @Resource
    private UserPointRecordDao userPointRecordDao;

    /**
     * 用户积分记录
     * @param timeType (1全部，2最近30天，3最近1年)
     * @param pageRequest
     * @return
     */
    @Override
    public CommonPage<UserIntegralRecodResponse> recordList(Integer timeType, PageParamRequest pageRequest) {
        Integer uid = userService.getUserIdException();

        PageHelper.startPage(pageRequest.getPage(), pageRequest.getLimit());


        LambdaQueryWrapper<UserPointRecord> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UserPointRecord::getUid,uid);
        DateTime nowTime = DateUtil.date();
        if(timeType == 2){
            DateTime offsetMonth = DateUtil.offsetDay(nowTime, -30);
            lqw.between(UserPointRecord::getCreateTime,offsetMonth,nowTime);
        }else if(timeType == 3){
            DateTime offsetYear = DateUtil.offsetDay(nowTime, -365);
            lqw.between(UserPointRecord::getCreateTime,offsetYear,nowTime);
        }
        lqw.orderByDesc(UserPointRecord::getId);
        List<UserPointRecord> userPointRecords = userPointRecordDao.selectList(lqw);

        CommonPage<UserPointRecord> userPointRecordCommonPage = CommonPage.restPage(userPointRecords);
        List<UserIntegralRecodResponse> responseList = CollUtil.newArrayList();
        for (UserPointRecord userPointRecord : userPointRecords) {
            UserIntegralRecodResponse userIntegralRecodResponse = new UserIntegralRecodResponse();
            BeanUtil.copyProperties(userPointRecord,userIntegralRecodResponse);
            for (IntegralTransactionType value : IntegralTransactionType.values()) {
                if(userPointRecord.getTransactionType().equals(value.getCode())){
                    userIntegralRecodResponse.setTransactionTypeStr(value.getInfo());
                    break;
                }
            }
            responseList.add(userIntegralRecodResponse);
        }

        CommonPage<UserIntegralRecodResponse> detailPage = CommonPage.restPage(responseList);
        BeanUtils.copyProperties(userPointRecordCommonPage, detailPage, "list");
        return detailPage;
    }
}
